﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;


namespace hash_a_popo_addIn
{
    public partial class ThisAddIn
    {
        private Office.CommandBar menuBar;
        private Office.CommandBarPopup newMenuBar;
        private Office.CommandBarButton verifyButton;
        private Office.CommandBarButton signatureButton;

        private String content = "";

        private string menuTag = "Hash-a-Popo";


        private void AddMenuBar() {
            try {
                
                //Define the existent Menu Bar
                menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;

                //Define the new Menu Bar into the old menu bar
                newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                    Office.MsoControlType.msoControlPopup, missing,
                    missing, missing, false);

                //If I dont find the newMenuBar, I add it
                if (newMenuBar != null)
                {
                    newMenuBar.Caption = "Hash-A-Popo";
                    newMenuBar.Tag = menuTag;
                    
                    verifyButton = (Office.CommandBarButton)newMenuBar.Controls.
                    Add(Office.MsoControlType.msoControlButton, missing,
                        missing, 1, true);
                    signatureButton = (Office.CommandBarButton)newMenuBar.Controls.
                    Add(Office.MsoControlType.msoControlButton, missing,
                        missing, 1, true);

                    verifyButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    signatureButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;

                    verifyButton.Caption = "Verify this email";
                    signatureButton.Caption = "Create signature";

                    //This is the Icon near the Text
                    //verifyButton.FaceId = 610;
                    //verifyButton.Tag = "c123";
                    //Insert Here the Button1.Click event    
                    newMenuBar.Visible = true;
                }
            }
            catch (Exception ex)
            {
                //This MessageBox is visible if there is an error
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message.ToString(), "Error Message Box", MessageBoxButtons.OK, 
                MessageBoxIcon.Exclamation);
            }
        }

        #region Remove Button from Outlook's Menu
        private void RemoveMenubar()
        {
            // If the menu already exists, remove it.
            try
            {
                Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                    this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                    FindControl(Office.MsoControlType.msoControlPopup,
                    missing, menuTag, true, true);
                if (foundMenu != null)
                {
                    foundMenu.Delete(true);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        #endregion


        #region eventHandler
        private void verifyButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
        {
            Console.WriteLine("hello world");
        }

        #endregion

        #region startUpShutDown
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            RemoveMenubar();
            AddMenuBar();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
        #endregion

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion

        private void verifyEmail()
        {
            
        }
    }
}
